<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QSqlRelationalTableModel Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QSqlRelationalTableModel Class Reference<br /><sup><sup>[<a href="qtsql.html">QtSql</a> module]</sup></sup></h1><p>The QSqlRelationalTableModel class provides an editable data
model for a single database table, with foreign key support.
<a href="#details">More...</a></p>

<p>Inherits <a href="qsqltablemodel.html">QSqlTableModel</a>.</p><h3>Types</h3><ul><li><div class="fn" />enum <b><a href="qsqlrelationaltablemodel.html#JoinMode-enum">JoinMode</a></b> { InnerJoin, LeftJoin }</li></ul><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#QSqlRelationalTableModel">__init__</a></b> (<i>self</i>, QObject&#160;<i>parent</i>&#160;=&#160;None, QSqlDatabase&#160;<i>db</i>&#160;=&#160;QSqlDatabase())</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#clear">clear</a></b> (<i>self</i>)</li><li><div class="fn" />QVariant <b><a href="qsqlrelationaltablemodel.html#data">data</a></b> (<i>self</i>, QModelIndex&#160;<i>index</i>, int&#160;<i>role</i>&#160;=&#160;Qt.DisplayRole)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.html#insertRowIntoTable">insertRowIntoTable</a></b> (<i>self</i>, QSqlRecord&#160;<i>values</i>)</li><li><div class="fn" />QString <b><a href="qsqlrelationaltablemodel.html#orderByClause">orderByClause</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlRelation <b><a href="qsqlrelationaltablemodel.html#relation">relation</a></b> (<i>self</i>, int&#160;<i>column</i>)</li><li><div class="fn" />QSqlTableModel <b><a href="qsqlrelationaltablemodel.html#relationModel">relationModel</a></b> (<i>self</i>, int&#160;<i>column</i>)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.html#removeColumns">removeColumns</a></b> (<i>self</i>, int&#160;<i>column</i>, int&#160;<i>count</i>, QModelIndex&#160;<i>parent</i>&#160;=&#160;QModelIndex())</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#revertRow">revertRow</a></b> (<i>self</i>, int&#160;<i>row</i>)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.html#select">select</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsqlrelationaltablemodel.html#selectStatement">selectStatement</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.html#setData">setData</a></b> (<i>self</i>, QModelIndex&#160;<i>index</i>, QVariant&#160;<i>value</i>, int&#160;<i>role</i>&#160;=&#160;Qt.EditRole)</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#setJoinMode">setJoinMode</a></b> (<i>self</i>, JoinMode&#160;<i>joinMode</i>)</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#setRelation">setRelation</a></b> (<i>self</i>, int&#160;<i>column</i>, QSqlRelation&#160;<i>relation</i>)</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#setTable">setTable</a></b> (<i>self</i>, QString&#160;<i>tableName</i>)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.html#updateRowInTable">updateRowInTable</a></b> (<i>self</i>, int&#160;<i>row</i>, QSqlRecord&#160;<i>values</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QSqlRelationalTableModel class provides an editable data
model for a single database table, with foreign key support.</p>
<p>QSqlRelationalTableModel acts like <a href="qsqltablemodel.html">QSqlTableModel</a>, but allows columns to be
set as foreign keys into other database tables.</p>
<table class="generic">
<tr class="odd" valign="top">
<td><img alt="" src="images/noforeignkeys.png" /></td>
<td><img alt="" src="images/foreignkeys.png" /></td>
</tr>
</table>
<p>The screenshot on the left shows a plain <a href="qsqltablemodel.html">QSqlTableModel</a> in a <a href="qtableview.html">QTableView</a>. Foreign keys (<tt>city</tt> and
<tt>country</tt>) aren't resolved to human-readable values. The
screenshot on the right shows a QSqlRelationalTableModel, with
foreign keys resolved into human-readable text strings.</p>
<p>The following code snippet shows how the
QSqlRelationalTableModel was set up:</p>
<pre class="cpp">
     model<span class="operator">-</span><span class="operator">&gt;</span>setTable(<span class="string">"employee"</span>);

     model<span class="operator">-</span><span class="operator">&gt;</span>setRelation(<span class="number">2</span><span class="operator">,</span> <span class="type"><a href="qsqlrelation.html">QSqlRelation</a></span>(<span class="string">"city"</span><span class="operator">,</span> <span class="string">"id"</span><span class="operator">,</span> <span class="string">"name"</span>));
     model<span class="operator">-</span><span class="operator">&gt;</span>setRelation(<span class="number">3</span><span class="operator">,</span> <span class="type"><a href="qsqlrelation.html">QSqlRelation</a></span>(<span class="string">"country"</span><span class="operator">,</span> <span class="string">"id"</span><span class="operator">,</span> <span class="string">"name"</span>));
</pre>
<p>The <a href="qsqlrelationaltablemodel.html#setRelation">setRelation</a>()
function calls establish a relationship between two tables. The
first call specifies that column 2 in table <tt>employee</tt> is a
foreign key that maps with field <tt>id</tt> of table
<tt>city</tt>, and that the view should present the <tt>city</tt>'s
<tt>name</tt> field to the user. The second call does something
similar with column 3.</p>
<p>If you use a read-write QSqlRelationalTableModel, you probably
want to use <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a> on the
view. Unlike the default delegate, <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a> provides a
combobox for fields that are foreign keys into other tables. To use
the class, simply call <a href="qabstractitemview.html#setItemDelegate">QAbstractItemView.setItemDelegate</a>()
on the view with an instance of <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a>:</p>
<pre class="cpp">
     <span class="type"><a href="qtableview.html">QTableView</a></span> <span class="operator">*</span>view <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtableview.html">QTableView</a></span>;
     view<span class="operator">-</span><span class="operator">&gt;</span>setModel(model);
     view<span class="operator">-</span><span class="operator">&gt;</span>setItemDelegate(<span class="keyword">new</span> <span class="type"><a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a></span>(view));
</pre>
<p>The <a href="sql-relationaltablemodel.html">sql/relationaltablemodel</a>
example illustrates how to use QSqlRelationalTableModel in
conjunction with <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a> to provide
tables with foreign key support.</p>
<p class="centerAlign"><img alt="" src="images/relationaltable.png" /></p>
<p>Notes:</p>
<ul>
<li>The table must have a primary key declared.</li>
<li>The table's primary key may not contain a relation to another
table.</li>
<li>If a relational table contains keys that refer to non-existent
rows in the referenced table, the rows containing the invalid keys
will not be exposed through the model. The user or the database is
responsible for keeping referential integrity.</li>
<li>If a relation's display column name is also used as a column
name in the main table, or if it is used as display column name in
more than one relation it will be aliased. The alias is is the
relation's table name and display column name joined by an
underscore (e.g. tablename_columnname). All occurrences of the
duplicate display column name are aliased when duplication is
detected, but no aliasing is done to the column names in the main
table. The aliasing doesn't affect <a href="qsqlrelation.html">QSqlRelation</a>, so <a href="qsqlrelation.html#displayColumn">QSqlRelation.displayColumn</a>()
will return the original display column name, but <a href="qsqlrecord.html#fieldName">QSqlRecord.fieldName</a>() will return
aliases.</li>
<li>When using <a href="qsqlrelationaltablemodel.html#setData">setData</a>() the role
should always be <a href="qt.html#ItemDataRole-enum">Qt.EditRole</a>, and when using
<a href="qsqlrelationaltablemodel.html#data">data</a>() the role
should always be <a href="qt.html#ItemDataRole-enum">Qt.DisplayRole</a>.</li>
</ul>
<hr /><h2>Type Documentation</h2><h3 class="fn"><a name="JoinMode-enum" />QSqlRelationalTableModel.JoinMode</h3><p>This enum specifies the type of mode to use when joining two
tables.</p>
<table class="valuelist">
<tr class="odd" valign="top">
<th class="tblConst">Constant</th>
<th class="tblval">Value</th>
<th class="tbldscr">Description</th>
</tr>
<tr>
<td class="topAlign">
<tt>QSqlRelationalTableModel.InnerJoin</tt></td>
<td class="topAlign"><tt>0</tt></td>
<td class="topAlign">Inner join mode, return rows when there is at
least one match in both tables.</td>
</tr>
<tr>
<td class="topAlign">
<tt>QSqlRelationalTableModel.LeftJoin</tt></td>
<td class="topAlign"><tt>1</tt></td>
<td class="topAlign">Left join mode, returns all rows from the left
table (table_name1), even if there are no matches in the right
table (table_name2).</td>
</tr>
</table>
<p>This enum was introduced or modified in Qt 4.8.</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#setJoinMode">QSqlRelationalTableModel.setJoinMode</a>().</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSqlRelationalTableModel" />QSqlRelationalTableModel.__init__ (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>parent</i>&#160;=&#160;None, <a href="qsqldatabase.html">QSqlDatabase</a>&#160;<i>db</i>&#160;=&#160;QSqlDatabase())</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Creates an empty <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> and
sets the parent to <i>parent</i> and the database connection to
<i>db</i>. If <i>db</i> is not valid, the default database
connection will be used.</p>


<h3 class="fn"><a name="clear" />QSqlRelationalTableModel.clear (<i>self</i>)</h3><p>Reimplemented from <a href="qsqlquerymodel.html#clear">QSqlQueryModel.clear</a>().</p>


<h3 class="fn"><a name="data" />QVariant QSqlRelationalTableModel.data (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>index</i>, int&#160;<i>role</i>&#160;=&#160;Qt.DisplayRole)</h3><p>Reimplemented from <a href="qabstractitemmodel.html#data">QAbstractItemModel.data</a>().</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#setData">setData</a>().</p>


<h3 class="fn"><a name="insertRowIntoTable" />bool QSqlRelationalTableModel.insertRowIntoTable (<i>self</i>, <a href="qsqlrecord.html">QSqlRecord</a>&#160;<i>values</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#insertRowIntoTable">QSqlTableModel.insertRowIntoTable</a>().</p>


<h3 class="fn"><a name="orderByClause" />QString QSqlRelationalTableModel.orderByClause (<i>self</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#orderByClause">QSqlTableModel.orderByClause</a>().</p>


<h3 class="fn"><a name="relation" /><a href="qsqlrelation.html">QSqlRelation</a> QSqlRelationalTableModel.relation (<i>self</i>, int&#160;<i>column</i>)</h3><p>Returns the relation for the column <i>column</i>, or an invalid
relation if no relation is set.</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#setRelation">setRelation</a>() and
<a href="qsqlrelation.html#isValid">QSqlRelation.isValid</a>().</p>


<h3 class="fn"><a name="relationModel" /><a href="qsqltablemodel.html">QSqlTableModel</a> QSqlRelationalTableModel.relationModel (<i>self</i>, int&#160;<i>column</i>)</h3><p>Returns a <a href="qsqltablemodel.html">QSqlTableModel</a>
object for accessing the table for which <i>column</i> is a foreign
key, or 0 if there is no relation for the given <i>column</i>.</p>
<p>The returned object is owned by the <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a>.</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#setRelation">setRelation</a>() and
<a href="qsqlrelationaltablemodel.html#relation">relation</a>().</p>


<h3 class="fn"><a name="removeColumns" />bool QSqlRelationalTableModel.removeColumns (<i>self</i>, int&#160;<i>column</i>, int&#160;<i>count</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>parent</i>&#160;=&#160;QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#removeColumns">QAbstractItemModel.removeColumns</a>().</p>


<h3 class="fn"><a name="revertRow" />QSqlRelationalTableModel.revertRow (<i>self</i>, int&#160;<i>row</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#revertRow">QSqlTableModel.revertRow</a>().</p>


<h3 class="fn"><a name="select" />bool QSqlRelationalTableModel.select (<i>self</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#select">QSqlTableModel.select</a>().</p>


<h3 class="fn"><a name="selectStatement" />QString QSqlRelationalTableModel.selectStatement (<i>self</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#selectStatement">QSqlTableModel.selectStatement</a>().</p>


<h3 class="fn"><a name="setData" />bool QSqlRelationalTableModel.setData (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>index</i>, QVariant&#160;<i>value</i>, int&#160;<i>role</i>&#160;=&#160;Qt.EditRole)</h3><p>Reimplemented from <a href="qabstractitemmodel.html#setData">QAbstractItemModel.setData</a>().</p>
<p>Sets the data for the <i>role</i> in the item with the specified
<i>index</i> to the <i>value</i> given. Depending on the edit
strategy, the value might be applied to the database at once, or it
may be cached in the model.</p>
<p>Returns true if the value could be set, or false on error (for
example, if <i>index</i> is out of bounds).</p>
<p>For relational columns, <i>value</i> must be the index, not the
display value. The index must also exist in the referenced table,
otherwise the function returns false.</p>
<p><b>See also</b> <a href="qsqltablemodel.html#editStrategy">editStrategy</a>(), <a href="qsqlrelationaltablemodel.html#data">data</a>(), <a href="qsqltablemodel.html#submit">submit</a>(), and <a href="qsqlrelationaltablemodel.html#revertRow">revertRow</a>().</p>


<h3 class="fn"><a name="setJoinMode" />QSqlRelationalTableModel.setJoinMode (<i>self</i>, <a href="qsqlrelationaltablemodel.html#JoinMode-enum">JoinMode</a>&#160;<i>joinMode</i>)</h3><p>Sets the SQL join mode to the value given by <i>joinMode</i> to
show or hide rows with NULL foreign keys.</p>
<p>In <a href="qsqlrelationaltablemodel.html#JoinMode-enum">InnerJoin</a> mode
(the default) these rows will not be shown; use the <a href="qsqlrelationaltablemodel.html#JoinMode-enum">LeftJoin</a> mode if
you want to show them.</p>
<p>This function was introduced in Qt 4.8.</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#JoinMode-enum">QSqlRelationalTableModel.JoinMode</a>.</p>


<h3 class="fn"><a name="setRelation" />QSqlRelationalTableModel.setRelation (<i>self</i>, int&#160;<i>column</i>, <a href="qsqlrelation.html">QSqlRelation</a>&#160;<i>relation</i>)</h3><p>Lets the specified <i>column</i> be a foreign index specified by
<i>relation</i>.</p>
<p>Example:</p>
<pre class="cpp">
     model<span class="operator">-</span><span class="operator">&gt;</span>setTable(<span class="string">"employee"</span>);

     model<span class="operator">-</span><span class="operator">&gt;</span>setRelation(<span class="number">2</span><span class="operator">,</span> <span class="type"><a href="qsqlrelation.html">QSqlRelation</a></span>(<span class="string">"city"</span><span class="operator">,</span> <span class="string">"id"</span><span class="operator">,</span> <span class="string">"name"</span>));
</pre>
<p>The setRelation() call specifies that column 2 in table
<tt>employee</tt> is a foreign key that maps with field <tt>id</tt>
of table <tt>city</tt>, and that the view should present the
<tt>city</tt>'s <tt>name</tt> field to the user.</p>
<p>Note: The table's primary key may not contain a relation to
another table.</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#relation">relation</a>().</p>


<h3 class="fn"><a name="setTable" />QSqlRelationalTableModel.setTable (<i>self</i>, QString&#160;<i>tableName</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#setTable">QSqlTableModel.setTable</a>().</p>


<h3 class="fn"><a name="updateRowInTable" />bool QSqlRelationalTableModel.updateRowInTable (<i>self</i>, int&#160;<i>row</i>, <a href="qsqlrecord.html">QSqlRecord</a>&#160;<i>values</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#updateRowInTable">QSqlTableModel.updateRowInTable</a>().</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;snapshot-4.9.5-9eb6aac99275 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt&#160;4.8.3</td></tr></table></div></address></body></html>